<?php

/**
 * @copyright (c) 2011
 */
App::uses('AppModel', 'Model');

class StudentWallet extends AppModel {

    var $name = 'StudentWallet';
    
    public $belongsTo = array(
        'StudentUser' => array(
            'className' => 'StudentUser',
            'foreignKey' => 'student_user_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        ),
        'Bank' => array(
            'className' => 'Bank',
            'foreignKey' => 'bank_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        )
    );


    public function save($data = null,$validate = true, $fieldList = array()) {
        $s = $this->sanitize($data);
        return parent::save($s);
    }


    function getBalanceBreakDown($student_id){
        $return = null;
        $conditions = array('student_user_id'=>$student_id);
        $d = $this->find('all',array(
            'fields'=>array('StudentWallet.id','StudentWallet.bank_id','StudentWallet.account_balance'),
            'conditions'=>$conditions,
            'contain'=>array('Bank'=>array('fields'=>array('Bank.id','Bank.bank','Bank.bank_full'))),
            'recursive'=>1
        ));

        if($d){
            $return = $d;
        }
        else{//Add the bank wallet for each.
            //Get the existing banks wallets records
            $exw = $this->find('list',array(
                'fields'=>array('bank_id'),
                'conditions'=>array('student_user_id'=>$student_id),
                'recursive'=>-1
            ));
            if($exw){

            }
            else{
                $exw = array();
            }
            $bank_ids = ClassRegistry::init('Bank')->getBanksIds();
            $new_banks = array();
            foreach($bank_ids as $key => $value){
                if(in_array($value,$exw)){
                    //Skip
                }
                else{
                    $new_banks[] =$value;
                }
            }
            if(!empty($new_banks)){
                //Create the save data
                $save = array();
                foreach($new_banks as $bnk_id){
                    $save[]=array(
                        'id'=>0,
                        'student_user_id'=>$student_id,
                        'bank_id'=>$bnk_id,
                        'account_balance'=>0
                    );
                }
                $this->saveAll($save);
            }

            $d = $this->find('all',array(
                'fields'=>array('StudentWallet.id','StudentWallet.bank_id','StudentWallet.account_balance'),
                'conditions'=>$conditions,
                'contain'=>array('Bank'=>array('fields'=>array('Bank.id','Bank.bank','Bank.bank_full'))),
                'recursive'=>1
            ));

            $return = $d;
        }

        //format it
        $save = array();
        foreach($return as $bal){
            $save[$bal['StudentWallet']['bank_id']]=array(
                'bank_id'=>$bal['Bank']['id'],
                'bank_name'=>$bal['Bank']['bank'],
                'bank_full_name'=>$bal['Bank']['bank_full'],
                'amount'=>$bal['StudentWallet']['account_balance'],
            );
        }

        return $save;
    }


    function getBalance($student_id,$bank_id = null){
        $balance = 0.00;
        $conditions = array('student_user_id'=>$student_id);
        if($bank_id != null){
            $conditions['bank_id'] = $bank_id;
        }
        $d = $this->find('all',array(
            'fields'=>array('SUM(account_balance) AS total_bal'),
            'conditions'=>$conditions,
            'group'=>array('student_user_id'),
            'recursive'=>-1
        ));

        if($d){
            $balance = $d[0][0]['total_bal'];
        }
        else{//Add the bank wallet for each.
            //Get the existing banks wallets records
            $exw = $this->find('list',array(
                'fields'=>array('bank_id'),
                'conditions'=>array('student_user_id'=>$student_id),
                'recursive'=>-1
            ));
            if($exw){

            }
            else{
                $exw = array();
            }
            $bank_ids = ClassRegistry::init('Bank')->getBanksIds();
            $new_banks = array();
            foreach($bank_ids as $key => $value){
                if(in_array($value,$exw)){
                    //Skip
                }
                else{
                    $new_banks[] =$value;
                }
            }
            if(!empty($new_banks)){
                //Create the save data
                $save = array();
                foreach($new_banks as $bnk_id){
                    $save[]=array(
                        'id'=>0,
                        'student_user_id'=>$student_id,
                        'bank_id'=>$bnk_id,
                        'account_balance'=>0
                    );
                }
                $this->saveAll($save);
            }
        }
        return $balance;
    }

    function creditWallet($student_id,$amount,$bank_id){
        $balance = $this->getBalance($student_id,$bank_id);
        $new_bal = doubleval($balance) + doubleval($amount);
        $result = $this->updateAll(array('account_balance' => $new_bal),array('student_user_id' => $student_id,'bank_id' => $bank_id));
        if($result){
            return $new_bal;
        }
        else{
            return false;
        }
    }

    function debitWallet($student_id,$amount,$bank_id){
        $balance = $this->getBalance($student_id,$bank_id);
        $new_bal = doubleval($balance) - doubleval($amount);
        $result = $this->updateAll(array('account_balance' => $new_bal),array('student_user_id' => $student_id,'bank_id' => $bank_id));
        if($result){
            return $new_bal;
        }
        else{
            return false;
        }
    }

}